package cn.edu.hunau.controller;

import cn.edu.hunau.common.BindingResultParseMessage;
import cn.edu.hunau.common.BusinessException;
import cn.edu.hunau.common.R;
import cn.edu.hunau.request.LoginReq;
import cn.edu.hunau.service.UserService;
import cn.edu.hunau.pojo.User;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author hb
 * @date 2024-04-15 10:38:41
 */
@RestController
@RequestMapping("/user")
public class UserManagerAction {

    @Resource
    private UserService userService;

    @RequestMapping("/{id}")
    public R queryById(@PathVariable("id") Integer id){
        User user = userService.findById(id);
        user.getNickName();
        return R.ok().data("u",user);
    }

    @RequestMapping("/checkTelphone")
    public R checkTelphone(String telphone){
        userService.checkUserExistsByTelphone(telphone);

        return R.ok();
    }

    @PostMapping("/login")
    public R login(@RequestBody @Valid LoginReq loginData, BindingResult bindResult){
         //判断请求参数是否存在格式错误
        if(bindResult.hasFieldErrors()){
            throw new BusinessException(BindingResultParseMessage.parseDefaultMessage(bindResult),400400);
        }

      User user=  userService.login(loginData);

        return R.ok().data("user",user);
    }



}
